package com.yjy.fuyao.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.message.entity.Message;
import com.yjy.fuyao.message.entity.MessageVO;
import com.yjy.fuyao.message.entity.ViewMessage;
import com.yjy.fuyao.message.mapper.MessageMapper;
import com.yjy.fuyao.message.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjy.fuyao.message.service.ViewMessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站信息 服务实现类
 * </p>
 *
 * @author yjy
 * @since 2022-01-02
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private ViewMessageService vmService;

    @Override
    public Map<String, Object> pageListByUid(Page<Message> pageParam, String userId) {

        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getToId,userId)
        .or()
        .eq(Message::getToId,"0");
        Map<String, Object> map = createData(pageParam, queryWrapper);
        return map;
    }

    @Override
    public Map<String, Object> pageList(Page<Message> pageParam) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getFromId,"1");
        Map<String, Object> map = createData(pageParam, queryWrapper);
        return map;
    }

    @Override
    public boolean removeMessageById(String id) {
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean getNewMessageByUId(String userId) {
        ViewMessage viewMessage = vmService.getById(userId);
        if (viewMessage == null) {
            viewMessage = new ViewMessage();
            viewMessage.setUserId(userId);
            viewMessage.setLastTime(LocalDateTime.of(0,1,1,0,0,0));
        }
        LocalDateTime nowTime = LocalDateTime.now();
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getFromId,"1")
                .gt(Message::getCreateTime, viewMessage.getLastTime())
                .le(Message::getCreateTime, LocalDateTime.now());
        List<Message> messages = baseMapper.selectList(queryWrapper);
        return messages.size() != 0;
    }

    @Override
    public void readMsg(String userId) {
        ViewMessage viewMessage = new ViewMessage();
        viewMessage.setUserId(userId);
        viewMessage.setLastTime(LocalDateTime.now());
        vmService.saveOrUpdate(viewMessage);
    }


    public Map<String, Object> createData(Page<Message> pageParam,LambdaQueryWrapper<Message> queryWrapper) {
        List<Message> records;
        long current,pages,size,total;
        boolean hasNext,hasPrevious;
        Map<String, Object> map = new HashMap<String, Object>();

        baseMapper.selectPage(pageParam, queryWrapper);

        records = pageParam.getRecords();
        current = pageParam.getCurrent();
        pages = pageParam.getPages();
        size = pageParam.getSize();
        total = pageParam.getTotal();
        hasNext = pageParam.hasNext();
        hasPrevious = pageParam.hasPrevious();

        List<MessageVO> messageVOs = new ArrayList<>();
        Map<String,Object> content;

        for (Message record:records) {
            content = JSONObject.parseObject(record.getContent(), Map.class);
            MessageVO messageVO = new MessageVO();
            messageVO.setId(record.getId());
            messageVO.setContent(content);
            messageVO.setTopic(record.getTopic());
            messageVO.setCreateTime(record.getCreateTime());
            messageVO.setStatus(record.getStatus());
            messageVO.setFromId(record.getFromId());
            messageVO.setToId(record.getToId());
            messageVOs.add(messageVO);
        }

        map.put("items", messageVOs);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
